﻿package com.saint.soundRecorder
{

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.media.Microphone;
	import flash.system.Security;
	import flash.system.SecurityPanel;
	import flash.text.TextField;

	/**
	* ...
	* @author Delfeld, copyright 2008
	*/
	public class SoundRecorder extends MovieClip {

		private static const SR_AUDIO_ALLOWED:String = "SRAudioAllowed";

		private var recordingAllowed:Boolean;
		private var mic:Microphone;
		private var activityMeter1:MovieClip;
		private var activityLevel1:TextField;
		

		public function SoundRecorder()
		{
			
			activityLevel1 = new TextField()
			activityLevel1.text = "test";
			addChild(activityLevel1);
			
			activityMeter1 = new Bar();
			addChild(activityMeter1);
			activityMeter1.x = 300;
			activityMeter1.y = 200;
			
			
			try
			{
				recordingAllowed = false;

				// setup mic
				mic = Microphone.getMicrophone();

				// confirm the mic settings first
				Security.showSettings(SecurityPanel.MICROPHONE);

				// listen for mic becoming active
				this.addEventListener(SR_AUDIO_ALLOWED, allowAudioHandler, false, 0, true);

				// call the mic popup, and confirm with user that they should use the mic.
				if (mic != null) {
					// kill (some) feedback
					mic.setUseEchoSuppression(true);
					// send ALL mic input to the speaker
					mic.setLoopBack(true);
					// listen for events
					mic.addEventListener(ActivityEvent.ACTIVITY, activityHandler, false, 0, true);
					mic.addEventListener(StatusEvent.STATUS, statusHandler, false, 0, true);
				}
			}
			catch (e:Error)
			{
				trace("(caught error) SoundRecorder.SoundRecorder: " + e.toString() + " :\n.\n" + e.getStackTrace());
			}
		}

		// method is run when mic is activated
		private function allowAudioHandler(e:Event):void
		{
			e.target.removeEventListener(e.type, allowAudioHandler);

			setAudio();
		}

		// initial audio setup when the microphone is active
		private function setAudio():void
		{
			// this is in case of repeated calls to this method
			if (hasEventListener(Event.ENTER_FRAME))
			{
				removeEventListener(Event.ENTER_FRAME, changeDisplay);
			}

			mic.gain = 40;
			mic.rate = 16;
			mic.setSilenceLevel(5, 1000);

			// start displaying visuals on every frame
			addEventListener(Event.ENTER_FRAME, changeDisplay, false, 0, true);
		}

		private function changeDisplay(e:Event):void
		{
			if (recordingAllowed)
			{
				
				
				trace(mic.activityLevel.toFixed(0));
				// resize the meter (really simple feedback)
				activityMeter1.height =  mic.activityLevel;
				// note the mic level
				activityLevel1.text = mic.activityLevel.toFixed(0);
			}
		}

		private function activityHandler(event:ActivityEvent):void {
			//trace("activityHandler: " + event);
		}

		private function statusHandler(event:StatusEvent):void {
			if (event.code == "Microphone.Unmuted")
			{
				//trace("Microphone access was allowed.");
				recordingAllowed = true;
				dispatchEvent(new Event(SR_AUDIO_ALLOWED));
			}
			else if (event.code == "Microphone.Muted")
			{
				//trace("Microphone access was denied.");
				recordingAllowed = false;
			}
		}
	}
}